NB: Before I begin my exercise I was highly influenced by Ozancan Ozdemir (http://users.metu.edu.tr/ozancan/) from METU in constructing my visualizations.
library(tidyverse)
library(sp)
library(readxl)
library(maps)
library(mapproj)
library(viridis)
Getting data for Turkey
TUR <- readRDS("TUR_adm1.rds")
TUR@data %>% as_tibble() %>% head(10)
## # A tibble: 10 x 13
## OBJECTID ID_0 ISO NAME_0 ID_1 NAME_1 HASC_1 CCN_1 CCA_1 TYPE_1
## <int> <int> <chr> <chr> <int> <chr> <chr> <int> <chr> <chr>
## 1 1 235 TUR Turkey 1 Çanak… TR.CK NA "" Il
## 2 2 235 TUR Turkey 2 Çanki… TR.CI NA "" Il
## 3 3 235 TUR Turkey 3 Çorum TR.CM NA "" Il
## 4 4 235 TUR Turkey 4 Adana TR.AA NA "" Il
## 5 5 235 TUR Turkey 5 Adiya… TR.AD NA "" Il
## 6 6 235 TUR Turkey 6 Afyon TR.AF NA "" Il
## 7 7 235 TUR Turkey 7 Agri TR.AG NA "" Il
## 8 8 235 TUR Turkey 8 Aksar… TR.AK NA "" Il
## 9 9 235 TUR Turkey 9 Amasya TR.AM NA "" Il
## 10 10 235 TUR Turkey 10 Ankara TR.AN NA "" Il
## # … with 3 more variables: ENGTYPE_1 <chr>, NL_NAME_1 <chr>,
## # VARNAME_1 <chr>
As you can see the province names are a bit wierd, but we move on.
#Just a map of Turkey to see if code is working
ggplot(TUR, aes(x = long, y = lat)) +geom_polygon(aes(group = group)) + coord_fixed()
Separate cities using fortify and make a nice map
TUR_for <- fortify(TUR)
ggplot(TUR_for) + geom_polygon(aes(x = long, y = lat, group = group), color = "white", fill = "#97ECEA") +theme_void() + coord_fixed()
#load the dataset with data from arnotrlari.com
setwd("/Users/mufitcan/Desktop")
export_turkey <- read_excel("export_turkey.xlsx")
#Give id numbers to cities
id_and_cities<- data_frame(id = rownames(TUR@data), Il = TUR@data$NAME_1) %>% left_join(export_turkey , by = "Il")
#Creating Dataset for making the map
map_data <- left_join(TUR_for, id_and_cities, by = "id")
Now that we have the data in order I can create a static map. Basically the same map on arnotlar.com
ihracat_azalma_turkiye <-ggplot(map_data, aes(x = long, y = lat, group = group, fill = Ihracat_azalma,
text = paste("Il:", Il,"<br>", "Azalma Oranı:", Ihracat_azalma, "<br>", "1. Ihracat sektörü:", En_buyuk,"<br>", "2. Ihracat sektörü:", Ikinci_buyuk ))) +
geom_polygon(color = "grey") +
coord_map() +theme_void() +
labs(title = "Azalan ihracatın aylık şehir GSMH’sına oranı",caption = "Akçiğit ve Akgündüz (2020)") +
scale_fill_distiller(name = "Azalan ihracat oranı", palette = "Reds", limits = c(-100,10), na.value = "white") +
theme(plot.title = element_text(hjust = 0.5),plot.subtitle = element_text(hjust = 0.5))
ihracat_azalma_turkiye
Try and make the graph interactive
#load necessary libraries for interactive map
library(plotly)
library(htmlwidgets)
library(widgetframe)
turkiye_interactive <- ggplotly(ihracat_azalma_turkiye, tooltip = "text")
turkiye_interactive